<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="handleSearchList()"
          type="primary"
          size="s米糕"
        >
          查询结果
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="s米糕"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="s米糕"
          label-width="140px"
        >
          <el-form-item label="输入搜索：">
            <el-input
              style="width: 203px"
              v-model="listQuery.tradename"
              placeholder="商品名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="tokenId：">
            <el-input
              style="width: 203px"
              type="number"
              v-model="listQuery.tokenId"
              placeholder="tokenId"
            ></el-input>
          </el-form-item>
          <el-form-item label="商品分类：">
            <popupTreeInput
              :props.sync="searchTreeProps"
              :currentNode.sync="currentNftType"
              :data="productCateOptions"
              :currentChangeHandle="currentChangeHandle"
            />
          </el-form-item>
          <!-- <el-form-item label="商品品牌：">
            <el-select v-model="listQuery.brandId" placeholder="请选择品牌" clearable>
              <el-option v-for="item in brandOptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item> -->
          <el-form-item label="上架状态：">
            <el-select v-model="listQuery.shelves" placeholder="全部" clearable>
              <el-option
                v-for="item in publishStatusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="审核状态：">
            <el-select v-model="listQuery.verifyStatus" placeholder="全部" clearable>
              <el-option v-for="item in verifyStatusOptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item> -->
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button class="btn-add" @click="handleAddProduct()" size="mini">
        添加
      </el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="productTable"
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column>
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.mid }}</template>
        </el-table-column>
        <el-table-column label="商品图片" width="120" align="center">
          <template slot-scope="scope"
            ><img style="height: 80px" :src="scope.row.pictures"
          /></template>
        </el-table-column>
        <el-table-column label="盲盒名称" align="center">
          <template slot-scope="scope">
            <p>{{ scope.row.tradename }}</p>
          </template>
        </el-table-column>
        <el-table-column label="作者" align="center">
          <template slot-scope="scope">
            <p>{{ scope.row.author }}</p>
          </template>
        </el-table-column>
        <el-table-column label="价格/tokenId：" width="120" align="center">
          <template slot-scope="scope">
            <p>商品组价格：￥{{ scope.row.price }}</p>
            <p>tokenId：{{ scope.row.tokenId }}</p>
          </template>
        </el-table-column>
        <el-table-column label="标签" width="140" align="center">
          <template slot-scope="scope">
            <p>
              上架：
              <el-switch
                @change="handlePublishStatusChange(scope.$index, scope.row)"
                :active-value="1"
                :inactive-value="0"
                v-model="scope.row.status"
              >
              </el-switch>
            </p>
            <p>
              热门：
              <el-switch
                @change="handleNewStatusChange(scope.$index, scope.row)"
                :active-value="1"
                :inactive-value="0"
                v-model="scope.row.hot"
              >
              </el-switch>
            </p>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="260" align="center">
          <template slot-scope="scope">
            <p>
              <el-button
                size="mini"
                @click="handleShowProduct(scope.$index, scope.row)"
                >查看系列nft
              </el-button>
              <el-button
                size="mini"
                @click="handleUpdateProduct(scope.$index, scope.row)"
                >编辑
              </el-button>
            </p>
            <p>
              <!-- <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除
              </el-button> -->
            </p>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="batch-operate-container">
      <el-select size="s米糕" v-model="operateType" placeholder="批量操作">
        <el-option
          v-for="item in operates"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-button
        style="margin-left: 20px"
        class="search-button"
        @click="handleBatchOperate()"
        type="primary"
        size="s米糕"
      >
        确定
      </el-button>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total,prev, pager, next,jumper"
        :page-size="size"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- <listByProductDetails ref="listByProductDetails" @closeDialog="closeDialog" :dialogVisible="dialogVisible" ></listByProductDetails> -->
  </div>
</template>
<script>
import {
  fetchList,
  updateDeleteStatus,
  updateNewStatus,
  updateRecommendStatus,
  updatePublishStatus,
  getListBySearchRequest,
  updateHot,
} from "../../../api/product";

import PopupTreeInput from "../../../components/TreeSearch/index.vue";
import {
  OPERATION_STATUS_ARRAY,
  PUBLISH_STATUS_OPTIONS,
} from "../../../contanst/data";
import { fetchList as fetchBrandList } from "@/api/brand";
import { fetchListWithChildren } from "@/api/productCate";
import { OSS_URL } from "../../../contanst/url";
import { Storage } from "../../../store/storage";
import { handleCurrentChange, handleSizeChange } from "../../../page/page";
import { DefaultListQuery } from "../../../entity/market";
import { handleSelectionChange } from "../../../checkbox/handler";
export default {
  name: "productList",
  data() {
    return {
      editSkuInfo: {
        dialogVisible: false,
        productId: null,
        productSn: "",
        productAttributeCategoryId: null,
        stockList: [],
        productAttr: [],
        keyword: null,
      },
      operates: OPERATION_STATUS_ARRAY,
      operateType: null,
      listQuery: Object.assign({}, DefaultListQuery),
      list: null,
      nftTypeId: 0,
      searchTreeProps: {},
      total: 0,
      size: 15,
      currentNftType: "",
      listLoading: true,
      dialogVisible: false,
      selectProductCateValue: null,
      multipleSelection: [],
      productCateOptions: [],
      brandOptions: [],
      publishStatusOptions: PUBLISH_STATUS_OPTIONS,
    };
  },

  components: {
    popupTreeInput: PopupTreeInput,
    // listByProductDetails: () => import("./components/index/ListByProductDetails.vue")
  },

  created() {
    this.getList();
    // this.getBrandList();
    this.getProductCateList();
  },
  watch: {
    selectProductCateValue: function (newValue) {
      if (newValue != null && newValue.length == 2) {
        this.listQuery.productCategoryId = newValue[1];
      } else {
        this.listQuery.productCategoryId = null;
      }
    },
    dialogVisible(val) {
      this.dialogVisible = val;
    },
  },

  methods: {
    handleCurrentChange,
    handleSelectionChange,
    handleSizeChange,
    closeDialog(value) {
      console.log("value", value);

      this.$set(this, "dialogVisible", value);
    },
    /**
     * 树形菜单选择
     */
    currentChangeHandle(data) {
      this.currentNftType = data.label;
      this.nftTypeId = data.value;
      this.listQuery.nftType = data.value;
    },

    getProductSkuSp(row, index) {
      let spData = JSON.parse(row.spData);
      if (spData != null && index < spData.length) {
        return spData[index].value;
      } else {
        return null;
      }
    },

    getResponse(response) {
      this.listLoading = false;
      this.list = response.data.data;
      this.list.forEach((item) => {
        item.pictures = OSS_URL + item.pictures;
      });

      this.total = response.data.total;
      this.size = response.data.pageSize;
      Storage.marketnftByStorage(this.list);
    },

    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then((r) => this.getResponse(r));
    },
    getBrandList() {
      fetchBrandList({ pageNum: 1, pageSize: 100 }).then((response) => {
        this.brandOptions = [];
        let brandList = response.data.data;
        for (let i = 0; i < brandList.length; i++) {
          this.brandOptions.push({
            label: brandList[i].name,
            value: brandList[i].id,
          });
        }
      });
    },
    getProductCateList() {
      fetchListWithChildren().then((response) => {
        this.productCateOptions = response;
      });
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getListBySearch();
    },

    getListBySearch() {
      if (0 != this.nftTypeId) {
        this.listQuery.nftType = this.nftTypeId;
      }
      getListBySearchRequest(this.listQuery).then((r) => this.getResponse(r));
    },
    handleAddProduct() {
      this.$router.push({ path: '/pms/addBlindBox' });
    },
    handleBatchOperate() {
      if (this.operateType == null) {
        this.$message({
          message: "请选择操作类型",
          type: "warning",
          duration: 1000,
        });
        return;
      }
      if (this.multipleSelection == null || this.multipleSelection.length < 1) {
        this.$message({
          message: "请选择要操作的商品",
          type: "warning",
          duration: 1000,
        });
        return;
      }
      this.$confirm("是否要进行该批量操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let ids = [];

        console.log(this.multipleSelection);
        for (let i = 0; i < this.multipleSelection.length; i++) {
          ids.push(this.multipleSelection[i].mid);
        }
        switch (this.operateType) {
          case this.operates[0].value:
            this.updatePublishStatus(1, ids);
            break;
          case this.operates[1].value:
            this.updatePublishStatus(0, ids);
            break;
          case this.operates[2].value:
            updateHot(1, ids);
            break;
          case this.operates[3].value:
            updateHot(0, ids);
            break;
          default:
            break;
        }
        this.getList();
      });
    },

    handlePublishStatusChange(index, row) {
      let ids = [];
      ids.push(row.mid);
      this.updatePublishStatus(row.status, ids);
    },
    handleNewStatusChange(index, row) {
      let ids = [];
      ids.push(row.mid);
      updateHot(row.hot, ids);
    },
    handleRecommendStatusChange(index, row) {
      let ids = [];
      ids.push(row.id);
      this.updateRecommendStatus(row.recommandStatus, ids);
    },
    handleResetSearch() {
      this.selectProductCateValue = [];
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleDelete(index, row) {
      this.$confirm("是否要进行删除操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let ids = [];
        ids.push(row.id);
        this.updateDeleteStatus(1, ids);
      });
    },
    handleUpdateProduct(index, row) {
      this.$router.push({ path: "/pms/updateProduct", query: { id: row.mid } });
    },
    handleShowProduct(index, row) {
      this.dialogVisible = true;
      console.log(row);
      this.$refs.listByProductDetails.pullData(row.mid);
    },
    handleShowVerifyDetail(index, row) {
      console.log("handleShowVerifyDetail", row);
    },
    handleShowLog(index, row) {
      console.log("handleShowLog", row);
    },
    updatePublishStatus(publishStatus, ids) {
      let params = new URLSearchParams();
      params.append("ids", ids);
      params.append("publishStatus", publishStatus);
      updatePublishStatus(params).then((response) => {
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000,
        });
      });
    },
    updateNewStatus(newStatus, ids) {
      let params = new URLSearchParams();
      params.append("ids", ids);
      params.append("newStatus", newStatus);
      updateNewStatus(params).then((response) => {
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000,
        });
      });
    },
    updateRecommendStatus(recommendStatus, ids) {
      let params = new URLSearchParams();
      params.append("ids", ids);
      params.append("recommendStatus", recommendStatus);
      updateRecommendStatus(params).then((response) => {
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000,
        });
      });
    },
    updateDeleteStatus(deleteStatus, ids) {
      let params = new URLSearchParams();
      params.append("ids", ids);
      params.append("deleteStatus", deleteStatus);
      updateDeleteStatus(params).then((response) => {
        this.$message({
          message: "删除成功",
          type: "success",
          duration: 1000,
        });
      });
      this.getList();
    },
  },
};
</script>
<style>
</style>


